package com.lib76.socket.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.lib76.socket.CommunicationServer;
import com.lib76.socket.SocketObject;
import com.lib76.socket.client.SidSocketClientRunner.Slot;
import com.lib76.socket.message.JsonSocketMessage;
import com.lib76.socket.model.SocketChannel;

public class SidSocketClient2Runner {

	public static void main(String[] args) {
		boolean result = false;
		result = CommunicationServer.messageProtocolInitial(CommunicationServer.class.getResourceAsStream("/protocol1.0.xml"), null);
		result = CommunicationServer.socketInitial(CommunicationServer.class.getResourceAsStream("/communication_client_config.xml"), null);

		if(result){
			SocketObject client =CommunicationServer.getSocketObject("sidClient");
			if(client.run()){
				SocketChannel channel = client.getClientChannel();
				JsonSocketMessage msg = (JsonSocketMessage)client.getRequestMessage("sidLogin");
				msg.writeString("sourceObject", "1:SID002:");
				msg.writeString("deviceIp", "127.0.0.1");
				
				List<Slot> slots = new ArrayList<Slot>();
				Random random = new Random();
				for(int i = 0;i<32;i++){
					Long num = random.nextLong();
					slots.add( new Slot(""+i,num.toString()));
				}
				msg.writeList("slots", slots);
				
				if(channel.write(msg)){
					System.out.println("发送命令成功");
				}else{
					System.out.println("发送命令失败");
				}
			}
		}
	}

	public static class Slot{
		private String slotNum;
		private String slotImsi;
		
		public Slot(){}
		
		public Slot(String slotNum, String imsi) {
			super();
			this.slotNum = slotNum;
			this.slotImsi = imsi;
		}

		public String getSlotNum() {
			return slotNum;
		}
		public void setSlotNum(String slotNum) {
			this.slotNum = slotNum;
		}

		public String getSlotImsi() {
			return slotImsi;
		}

		public void setSlotImsi(String slotImsi) {
			this.slotImsi = slotImsi;
		}
	}
}
